//
//  MHMomentDetailViewModel.m
//  MiaoCircle
//
//  Created by 许正荣 on 2018/5/23.
//  Copyright © 2018年 acgera. All rights reserved.
//

#import "MHMomentDetailViewModel.h"

@implementation MHMomentDetailViewModel
-(void)initialize
{
    [super initialize];
    self.title  = @"详情";
    self.shouldRequestRemoteDataOnViewDidLoad = NO;
    self.shouldPullUpToLoadMore = YES;
    
//    self.shouldResignOnTouchOutside = YES;
    @weakify(self);
    RAC(self, moments) = self.requstRecentInfo.executionSignals.switchToLatest;
    
    RAC(self, comments) = self.requestRemoteDataCommand.executionSignals.switchToLatest;
    
    
    RAC(self,dataSource) = [RACObserve(self, moments) map:^(NSArray * moments) {
        @strongify(self)
        
        NSArray *arry = [self dataSourceWithMoment:moments];
    
        return arry;
    }];
    
    self.requstRecentInfo = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        if (self.isAskList) {
            
        }
        return  [[[[self.services.client getRecentStoryInfo:self.momentId] mh_parsedResults] map:^(id value) {
            self.moments = @[value];
            return @[value];
        }] doNext:^(id x) {
            [self requestRemoteDataSignalWithPage:1];
        }];
    }];
    
}

- (NSArray *)dataSourceWithMoment:(NSArray *)moments {
    if (MHObjectIsNil(moments) || moments.count == 0) return nil;
    NSMutableArray *viewModels = [[moments.rac_sequence map:^(MHMoment *moment) {
        MHMomentItemViewModel *itemViewModel = [[MHMomentItemViewModel alloc] initWithMoment:moment];
        itemViewModel.isDetail = true;
        itemViewModel.isRealateToMe = self.isRealateToMe;
//        itemViewModel.isCollection = self.isColleciton;
        itemViewModel.isFootPrint = self.isFootPrint;
        itemViewModel.expandOperationCmd = self.expandOperationCmd;
        itemViewModel.reloadSectionSubject = self.reloadSectionSubject;
        itemViewModel.commentSubject = self.commentSubject;
//        itemViewModel.profileInfoCommand = self.profileInfoCommand;
        itemViewModel.attributedTapCommand = self.attributedTapCommand;
        itemViewModel.shareTapCommand = self.shareTapCommand;
        itemViewModel.collectionCommand = self.collectionCommand;
        [itemViewModel _updateSubviewsFrameWithExpand:self.isDetail];
        return itemViewModel;
    }].array mutableCopy];
    return viewModels ?: @[] ;
}

- (instancetype)initWithServices:(id<MHViewModelServices>)services params:(NSDictionary *)params{
    if (self = [super initWithServices:services params:params]) {
        self.momentId = [params[MHViewModelIDKey] integerValue];
    }
    return self;
}



- (RACSignal *)requestRemoteDataSignalWithPage:(NSUInteger)page{
//    if (self.messageType == MHRecentMessageTypeAsk) {
//        return [RACSignal empty];
//    }else
//    {
    
        @weakify(self);
        NSArray * (^mapComment)(NSArray *) = ^(NSArray *comments) {
            @strongify(self);
            MHMoment *moment =  self.moments.firstObject;
        
            self.hiddenFooter = comments.count < self.perPage;
            if (page == 1) {
                comments = comments;
            }else
            {
                comments = @[(moment.commentsList ?: @[]).rac_sequence, comments.rac_sequence].rac_sequence.flatten.array;
            }
            
            moment.commentsList = [comments mutableCopy];
            self.moments = moment?@[moment]:@[];
            
            return comments;
        };

        return  [[self.services.client getCommentPagedListWithRecentId:self.momentId pageNum:page pageSize:self.perPage] map:mapComment];
//    }
    
   
}



@end
